package nl.weeaboo.sh.danmaku;

import java.awt.Rectangle;

import nl.weeaboo.sh.Game;
import nl.weeaboo.sh.danmaku.ColGrid.ColRectMode;
import nl.weeaboo.sh.level.Level;
import nl.weeaboo.sh.model.DamageRect;
import nl.weeaboo.sh.objects.HitFlash;
import nl.weeaboo.sh.objects.Sprite;

public class SpriteShotColHandler {

	private Game game;
	private Level level;
	private Rectangle bounds;
	
	public SpriteShotColHandler(Game g, Level l, Rectangle b) {
		game = g;
		level = l;
		bounds = b;
	}
	
	//Functions
	public void onCollision(Sprite s, ColRectMode m, ColNode cn, Rectangle a, Rectangle b) {		
		int ts = Level.tileSize;
		int dx = bounds.x-ts;
		int dy = bounds.y-ts;
		
		a.x += dx; a.y += dy;
		b.x += dx; b.y += dy;
		
		if (m == ColRectMode.HIT) {
			s.damage(cn.parent.getPower());
			if (!cn.parent.isSolid()) {
				cn.parent.kill();
			}				
		} else {
			DamageRect dr = (DamageRect)a;
			if (m == ColRectMode.DAMAGE && cn.parent.isSolid()) {
				Rectangle ha = a.intersection(b);
				s.onHitConnect(null, ha, dr);

				float r = SpellUtil.angleTo(
						s.getX(), s.getY(),
						b.x+b.width/2, b.y+b.height/2);
				
				level.addEffect(new HitFlash(game, level,
						ha.x+ha.width/2,
						ha.y+ha.height/2,
						SpellUtil.angleToDegrees(r)));
				
				//TODO: Sound effect										
			}
			
			cn.parent.damage(Math.round(s.getPower() * dr.damage));				
		}		

		a.x -= dx; a.y -= dy;
		b.x -= dx; b.y -= dy;
	}
	
	//Getters
		
	//Setters
	
}
